var app = getApp();
var QQMapWX = require('../libs/qqmap-wx-jssdk.js');
var qqmapsdk //腾讯位置服务
//获取位置权限
export const getLocation = () =>{
  qqmapsdk = new QQMapWX({
    key: app.globalData.mapKey //自己的key秘钥 http://lbs.qq.com/console/mykey.html 在这个网址申请
  });
  return new Promise((resolve, reject) => {
    wx.getLocation({
      type: 'gcj02',
      success: info => {
        const latitude = info.latitude
        const longitude = info.longitude
        qqmapsdk.reverseGeocoder({
          location: {
            latitude,
            longitude
          },
          success: function (res) {
            const location = {
              latitude,
              longitude,
              city: res.result.ad_info.city,
              province: res.result.ad_info.province,
            }
            wx.setStorage({key: 'location',data: location})
            // app.globalData.location = location
            resolve(location)
          },
          fail: function (err) {
            err.msg = '调用位置查询失败，请稍后再试'
            reject(err)
          }
        });
      },
      fail: res => {
        if (res.errMsg == "getLocation:fail auth deny") {
          res.status = 1 
          res.msg = '获取微信定位失败，请授权小程序获取位置信息'
          // 用户拒绝授权
        }else if (res.errCode < 0){
          res.status = 2
          res.msg = '获取微信定位超时，请稍后再试'
        }else{
          res.status = 3
          res.msg = '获取微信定位失败，请检查系统设置'
        }
        reject(res)
      }
    })
  })
}